/* 
 * File:   Surface.cpp
 * Author: agu
 * 
 * Created on May 18, 2014, 1:54 AM
 */

#include "client_Surface.h"
#include "client_InitializationException.h"

Surface::Surface() {
    this->surface = NULL;
}

Surface::Surface(const Surface& orig) {
    this->surface = orig.surface;
}

Surface::Surface(const string path) {
    this->surface = IMG_Load(path.c_str());
    if (this->surface == NULL) {
        throw InitializationException(IMG_GetError());
    }
}

Surface::Surface(SDL_Surface* sdlSurface) {
    this->surface = sdlSurface;
}

Surface::~Surface() {
    //Deallocate surface
    SDL_FreeSurface(this->surface);
    this->surface = NULL;
}

void Surface::load(SDL_Surface* sdlSurface) {
    this->surface = sdlSurface;
}

SDL_Surface* Surface::getSurfaceSDL() {
    return this->surface;
}

int Surface::getWidth() {
    return this->surface->w;
}

int Surface::getheight() {
    return this->surface->h;
}

void Surface::escale(int width, int height) {
    SDL_Surface *temp = SDL_CreateRGBSurface(this->surface->flags, width, height, 
                                             this->surface->format->BitsPerPixel,
                                             this->surface->format->Rmask, 
                                             this->surface->format->Gmask, 
                                             this->surface->format->Bmask, 
                                             this->surface->format->Amask);
    if (temp == NULL)
          throw InitializationException(SDL_GetError());
    SDL_BlitScaled(this->surface, NULL, temp, NULL);
    SDL_FreeSurface(this->surface);
    this->surface = temp;
}

int Surface::getX() {
    return this->surface->clip_rect.x;
}

int Surface::getY() {
    return this->surface->clip_rect.y;
}

void Surface::setPosition(int x, int y) {
    this->surface->clip_rect.x = x;
    this->surface->clip_rect.y = y;
}